/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.time;

import java.util.Calendar;

/**
 * use a duration to test if a player can be played at the moment
 * @author Himadri Talukder <himadri@digitalmenubox.com>
 */
public class Duration implements Comparable<Duration> {

    private Calendar startTime;
    private Calendar endTime;

    public Duration(Calendar start, Calendar end) {
        startTime = start;
        endTime = end;
    }

    public Calendar getEndTime() {
        return endTime;
    }

    public Calendar getStartTime() {
        return startTime;
    }

    public boolean isPlayableNow() {
        Calendar now = Calendar.getInstance();
        return (now.compareTo(this.startTime) >= 0 && now.compareTo(this.endTime) <= 0);
    }

    //returns true if the time now already passed the endTime of this duration
    public boolean isPassed() {
        Calendar now = Calendar.getInstance();
        return (now.compareTo(this.endTime) > 0);
    }

    public int compareTo(Duration d) {
        if (d.startTime.get(Calendar.YEAR) == this.startTime.get(Calendar.YEAR)
                && d.startTime.get(Calendar.MONTH) == this.startTime.get(Calendar.MONTH)
                && d.startTime.get(Calendar.DAY_OF_MONTH) == this.startTime.get(Calendar.DAY_OF_MONTH)
                && d.startTime.get(Calendar.HOUR_OF_DAY) == this.startTime.get(Calendar.HOUR_OF_DAY)
                && d.startTime.get(Calendar.MINUTE) == this.startTime.get(Calendar.MINUTE)
                && d.startTime.get(Calendar.SECOND) == this.startTime.get(Calendar.SECOND)) {
            //we have to do this test here because java create time with random ending digits..
            return 0;
        } else {
            return this.startTime.compareTo(d.getStartTime());
        }

    }

    public boolean withInDuration(Calendar time) {
        int hour = time.get(Calendar.HOUR_OF_DAY);
        int minute = time.get(Calendar.MINUTE);
        int second = time.get(Calendar.SECOND);

        int nowSecond = second + 60 * minute + 3600 * hour;//time of day in second
        int startSecond = startTime.get(Calendar.HOUR_OF_DAY) * 3600 + startTime.get(Calendar.MINUTE) * 60 + startTime.get(Calendar.SECOND);
        int endSecond = endTime.get(Calendar.HOUR_OF_DAY) * 3600 + endTime.get(Calendar.MINUTE) * 60 + endTime.get(Calendar.SECOND);

        if (this.extendsToNextDay(startTime, endTime)) {
          System.out.println("YES");
        	if (startSecond <= nowSecond || endSecond >= nowSecond) {
                return true;
            } else {
                return false;
            }
            
        } else {
        	 System.out.println("NO");
            if (startSecond <= nowSecond && endSecond >= nowSecond) {
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean extendsToNextDay(Calendar startTime, Calendar endTime) {
        /*      
    	System.out.println(((endTime.get(Calendar.HOUR_OF_DAY) - startTime.get(Calendar.HOUR_OF_DAY)) * 3600
        + (endTime.get(Calendar.MINUTE) - startTime.get(Calendar.MINUTE)) * 60
        + (endTime.get(Calendar.SECOND) - startTime.get(Calendar.SECOND))));
        */
        
        return !(((endTime.get(Calendar.HOUR_OF_DAY) - startTime.get(Calendar.HOUR_OF_DAY)) * 3600
                + (endTime.get(Calendar.MINUTE) - startTime.get(Calendar.MINUTE)) * 60
                + (endTime.get(Calendar.SECOND) - startTime.get(Calendar.SECOND)))
                > 0);
    }

    public boolean equals(Object o) {

        if ((o instanceof Duration)) {
            Duration d = (Duration) o;
            if (this.compareTo(d) == 0 && d.getEndTime().getTimeInMillis() / 1000 == this.getEndTime().getTimeInMillis() / 1000) {
                return true;
            }
        }
        //System.out.println((int)((Duration)o).getEndTime().getTimeInMillis()/1000);
        //System.out.println(((int)(this.getEndTime().getTimeInMillis())/1000));
        return false;
    }

    public int hashCode() {
        return this.startTime.hashCode();
    }

    public String toString() {
        return this.startTime.getTime().toString() + " ==> " + this.endTime.getTime().toString();
    }

    public static void main(String[] args) {
        Calendar f = Calendar.getInstance();
        Calendar t = Calendar.getInstance();
        f.set(2010, 8, 22, 10, 50, 10);
        t.set(2010, 8, 30, 20, 30, 10);
        Duration d = new Duration(f, t);
        Duration d2 = new Duration(t, f);
        System.out.println(d.equals(d2));
        System.out.println(d.compareTo(d2));
    }
}
